﻿using System;
using System.Reflection;
using System.Configuration;

namespace absf
{
    //建立User表，有两个字段id，name，分别写出它们的set和get方法
    class User
    {
        private int _id;
        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }
    //建立Department表，两个字段id，deptname，分别写出set和get方法
    class Department
    {
        private int _id;
        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _deptName;
        public string DeptName
        {
            get { return _deptName; }
            set { _deptName = value; }
        }
    }
    //访问User表的接口
    interface IUser
    {
        void Insert(User user);//插入user的方法

        User GetUser(int id);//根据id查user的方法
    }
    //具体产品，用于访问sql server的User
    class SqlserverUser : IUser
    {
        public void Insert(User user)
        {
            Console.WriteLine("在Sqlserver中给User表增加一条记录");
        }

        public User GetUser(int id)
        {
            Console.WriteLine("在Sqlserver中根据ID得到User表一条记录");
            return null;
        }
    }
    //具体产品，用于访问Access的User
    class AccessUser : IUser
    {
        public void Insert(User user)
        {
            Console.WriteLine("在Access中给User表增加一条记录");
        }

        public User GetUser(int id)
        {
            Console.WriteLine("在Access中根据ID得到User表一条记录");
            return null;
        }
    }
    //访问Department接口的表
    interface IDepartment
    {
        void Insert(Department department);//插入方法

        Department GetDepartment(int id);//根据id获取Department对象的方法
    }
    //具体产品，用于访问sql server的Department
    class SqlserverDepartment : IDepartment
    {
        public void Insert(Department department)
        {
            Console.WriteLine("在Sqlserver中给Department表增加一条记录");
        }

        public Department GetDepartment(int id)
        {
            Console.WriteLine("在Sqlserver中根据ID得到Department表一条记录");
            return null;
        }
    }
    //具体产品，用于访问Access的Department
    class AccessDepartment : IDepartment
    {
        public void Insert(Department department)
        {
            Console.WriteLine("在Access中给Department表增加一条记录");
        }

        public Department GetDepartment(int id)
        {
            Console.WriteLine("在Access中根据ID得到Department表一条记录");
            return null;
        }
    }
    //利用一个简单工厂+反射+配置文件进行数据库的访问
    class DataAccess
    {
        private static readonly string db=ConfigurationManager.AppSettings["DB"];//从配置文件中获取要访问的数据库类型
        private static readonly string AssemblyName="absf";//引入程序集名称
        //获取一个IUser的对象，调用数据库中的User表
        public static IUser CreateUser()
        {
            string className=AssemblyName+"."+db+"User";
            return (IUser)Assembly.Load(AssemblyName).CreateInstance(className);//利用反射实现数据访问程序
        }
        //获取一个IDepartment对象，调用数据库中的Department表
        public static IDepartment CreateDepartment()
        {
            string className=AssemblyName+"."+db+"Department";
            return (IDepartment)Assembly.Load(AssemblyName).CreateInstance(className);//利用反射实现数据访问程序
        }
    }
    //客户端
    class Program
    {
        static void Main(string[] args)
        {
            //定义两个表的对象
            User user = new User();
            Department dept = new Department();
            //从DataAccess工厂中获取IUser的值
            IUser iu = DataAccess.CreateUser();
            //调用User表的方法
            iu.Insert(user);
            iu.GetUser(1);
            //从DataAccess工厂中获取IDepartment的值
            IDepartment id = DataAccess.CreateDepartment();
            //调用Department表的方法
            id.Insert(dept);
            id.GetDepartment(1);

            Console.Read();
        }
    }


}